package com.bayss.core.service;

import com.bayss.core.constant.ResultMsg;
import com.bayss.core.entity.goods.ListClassify;
import com.bayss.core.entity.goods.ListClassifyExample;
import com.bayss.core.entity.goods.mapper.ListClassifyMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * Created by LDL on 2019/12/15.
 */
@Service
public class ListClassifyService {
    @Autowired
    private ListClassifyMapper listClassifyMapper;

    public PageInfo<ListClassify> findByPage(Integer page, Integer pageSize) {
        ListClassifyExample example = new ListClassifyExample();
        example.setOrderByClause("create_time desc");
        PageHelper.startPage(page, pageSize);
        PageInfo<ListClassify> result = PageHelper.startPage(page, pageSize).doSelectPageInfo(() -> listClassifyMapper.selectByExample(example));
        return result;
    }

    public List<ListClassify> list() {
        ListClassifyExample example = new ListClassifyExample();
        return listClassifyMapper.selectByExample(example);
    }

    /**
     * @param id
     * @return
     */
    public boolean defaultCheck(Integer id) {
        ListClassifyExample example = new ListClassifyExample();
        ListClassify listClassify = new ListClassify();
        listClassify.setDefaultValue(0);
        listClassifyMapper.updateByExampleSelective(listClassify, example);
        listClassify.setId(id);
        listClassify.setDefaultValue(1);
        return listClassifyMapper.updateByPrimaryKeySelective(listClassify) > 0;
    }

    public void deleteById(Integer id) {
        listClassifyMapper.deleteByPrimaryKey(id);
    }

    public void update(ListClassify listClassify) {
        listClassifyMapper.updateByPrimaryKeySelective(listClassify);
    }

    public ResultMsg add(ListClassify listClassify) {
        listClassify.setCreateTime(new Date());
        listClassify.setUpdateTime(new Date());
        listClassifyMapper.insertSelective(listClassify);
        return ResultMsg.success();
    }

}
